package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.thoughtworks.xstream.XStream;

public class ContactDataGenerator {


	public static void main(String[] args) throws IOException {
		if(args.length < 3) {
			System.out.println("Please specify parameters: <amount of test data> <file> <format>");
			return;
		}
		
		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2];
		
		if(file.exists()){
			System.out.println("File exists, please remote it manually: " + file);
			return;
		}
		
		List<ContactData> contacts = generateRandomContacts(amount);
		if("csv".equals(format)) {
			saveContactsToCsvFile(contacts, file);
		} else if("xml".equals(format)) {
			saveContactsToXmlFile(contacts, file);
		} else {
			System.out.println("Unknown format " + format);
			return;
		}
	}

	private static void saveContactsToXmlFile(List<ContactData> contacts, File file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		String xml = xstream.toXML(contacts);
		FileWriter writer = new FileWriter(file);
		writer.write(xml);
		writer.close();
	}
	
	public static List<ContactData> loadContactsFromXmlFile(File file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		return (List<ContactData>) xstream.fromXML(file);
	}

	private static void saveContactsToCsvFile(List<ContactData> contacts, File file) throws IOException {
		FileWriter writer = new FileWriter(file);
		for (ContactData contact : contacts) {
			writer.write(contact.getContactname() + "," + contact.getLastname() + "," + contact.getAddress()+ "," 
						+ contact.getAddress2() + "," + contact.getHomePhone() + "," + contact.getMobilePhone() + ","
						+ contact.getWorkPhone() + "," + contact.getHomePhone2() + "," + contact.getEmail() + ","
						+ contact.getEmail2() + "," + contact.getBmonth() + "," + contact.getBday() + "," 
						+ contact.getByear() + ",!" + "\n");
		}
		writer.close();
	}
	
	
	public static List<ContactData> loadContactsFromCsvFile(File file) throws IOException {
		List<ContactData> list = new ArrayList<ContactData>();
		FileReader reader = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(reader);
		String line = bufferedReader.readLine();
		while(line != null) {
			String[] part = line.split(",");
			ContactData contact = new ContactData()
			.withName(part[0])
			.withLastname(part[1])
			.withAddress(part[2])
			.withAddress2(part[3])
			.withHomePhone(part[4])
			.withMobilePhone(part[5])
			.withWorkPhone(part[6])
			.withHomePhone2(part[7])
			.withEmail(part[8])
			.withEmail2(part[9])
			.withBmonth(part[10])
			.withBday(part[11])
			.withByear(part[12]);
			list.add(contact);
			line = bufferedReader.readLine();
		}
		bufferedReader.close();
		return list;
	}

	public static List<ContactData> generateRandomContacts(int amount) {
		List<ContactData> list = new ArrayList<ContactData>();
		for (int i = 0; i < amount; i++){
			ContactData contact = new ContactData()
			.withName(generateRandomString())
			.withLastname(generateRandomString())
			.withAddress(generateRandomString())
			.withAddress2(generateRandomString())
			.withHomePhone(generateRandomPhone(7))
			.withMobilePhone(generateRandomPhone(11))
			.withWorkPhone(generateRandomPhone(7))
			.withHomePhone2(generateRandomPhone(7))
			.withEmail(generateRandomString())
			.withEmail2(generateRandomString())
			.withBmonth(generateRandomIndex(12, 1))
			.withBday(generateRandomIndex(31, 1))
			.withByear(generateRandomIndex(113, 1900));
			list.add(contact);
		}
		return list;
	}
	
	public static String generateRandomIndex(int range, int startIndex) {
		Random rnd = new Random();
		return Integer.toString(startIndex + rnd.nextInt(range));		
	}
	
	public static String generateRandomPhone(int count) {
		Random rnd = new Random();
		
		if (rnd.nextInt(5) == 0) {
			return "";
		} else {
			StringBuilder out = new StringBuilder();
			for (int i = 0; i < count; i++) {
				out.append(rnd.nextInt(10));
			}
			return out.toString();
		}
	}

	public static String generateRandomString() {
		Random rnd = new Random();
		if (rnd.nextInt(5) == 0) {
			return "";
		} else {
			return "test" + rnd.nextInt();
		}
	}
	
	
}
